<?php
class Zendvn_System_Author {
	
	protected $_mesageError;
	public function login($arrParam,$options = null){
		$db = Zend_Registry::get('connectDB');
		
		$authAdapter = new Zend_Auth_Adapter_DbTable($db, 'users','user_name','password');
		
		$select = $authAdapter->getDbSelect();
		$select->where('status = 1');
		$username = $arrParam['username'];
		$pass = new Zendvn_Encode();
		$password = $pass->password($arrParam['password']);
		
		$authAdapter->setIdentity($username);
		$authAdapter->setCredential($password);
		
		//2.Khoi tao Zend Auth
		$auth = Zend_Auth::getInstance();
		$result = $auth->authenticate($authAdapter);
		$flagAccess = false;
		if(!$result->isValid()){
			$msg = $result->getMessages();
			$this->_mesageError = current($msg);
		}else{
			
			$returnColumns = null;
			$omitColumns = array('password','active_code','status');
			$data = $authAdapter->getResultRowObject($returnColumns, $omitColumns);
			
			$auth->getStorage()->write($data);
			$flagAccess = true;
		}
		
		return $flagAccess;
	}
	
	public function getError(){
		return $this->_mesageError;
	}
	
	public function logout($arrParam = null,$options = null){
		$auth = Zend_Auth::getInstance();
		$auth->clearIdentity();
	}
}